function a(a) {
    return a && a.__esModule ? a : {
        default: a
    };
}

var e = a(require("../../api/api.js")), t = a(require("../../utils/util.js")), r = a(require("../../utils/filter.js")), o = require("../../config/constants.js"), s = getApp();

Page({
    data: {
        toView: "promo1",
        referral_list: [],
        referral_meta: {},
        bonus_paid_amount: 0,
        total_referrals: 0,
        referral_decrypto_id: "",
        hasMore: !1,
        page: 1,
        captchaText: "获取验证码",
        cellphone: "",
        captcha_code: "",
        timeFlag: 60,
        waiting: !0,
        userPersonalInfo: {},
        hasUserInfo: !1,
        isMe: !0,
        flag: !0
    },
    bindViewTap: function() {
        wx.navigateTo({
            url: "../logs/logs"
        });
    },
    redirectToTeacherPage: function() {
        wx.switchTab({
            url: "/pages/teacher/teacher"
        });
    },
    onLoad: function(a) {
        var e = this;
        a.encrypto ? this.getReferralDecrypto(a.encrypto) : this.getReferralHistory(), wx.getStorage({
            key: "userPersonalInfo",
            success: function(a) {
                e.setData({
                    userPersonalInfo: a.data,
                    hasUserInfo: !0
                });
            }
        });
    },
    show: function() {
        this.setData({
            flag: !1
        });
    },
    hide: function() {
        this.setData({
            flag: !0
        });
    },
    scroll: function(a) {
        a.detail.scrollLeft > 246 && a.detail.scrollLeft < 490 ? this.setData({
            toView: "promo2"
        }) : a.detail.scrollLeft > 491 && a.detail.scrollLeft < 735 ? this.setData({
            toView: "promo3"
        }) : a.detail.scrollLeft > 736 && a.detail.scrollLeft < 950 ? this.setData({
            toView: "promo4"
        }) : a.detail.scrollLeft > 950 ? this.setData({
            toView: "promo5"
        }) : this.setData({
            toView: "promo1"
        });
    },
    getReferralDecrypto: function(a) {
        var t = this, r = {
            encrypto_user_id: a
        }, o = this;
        e.default.getReferralDecrypto({
            data: r,
            success: function(a) {
                if (a && a.data.data) {
                    var e = a.data.data;
                    o.setData({
                        referral_decrypto_id: e.id
                    }), t.data.userPersonalInfo.id && e.id == t.data.userPersonalInfo.id ? (t.getReferralHistory(), 
                    o.setData({
                        isMe: !0
                    })) : o.setData({
                        isMe: !1
                    });
                }
            }
        });
    },
    onReachBottom: function(a) {
        var e = this;
        e.data.hasMore && e.getReferralHistory();
    },
    getReferralHistory: function() {
        var a = wx.getStorageSync("token") || "";
        if (a) {
            var r = this, o = {
                i_token: a,
                page: r.data.page
            };
            e.default.getReferralHistory({
                data: o,
                success: function(a) {
                    if (a && a.data) {
                        var e = r.data.referral_list;
                        1 === o.page && (e = []);
                        for (var s = a.data, n = 0; n < s.data.length; n++) s.data[n].update_time = t.default.amData(s.data[n].update_time);
                        var i = s.data, l = s.meta;
                        i && i.length >= 0 && (s.meta.has_next ? r.setData({
                            referral_list: e.concat(i),
                            referral_meta: l,
                            bonus_paid_amount: l.bonus_paid_amount,
                            total_referrals: l.total_referrals,
                            hasMore: !0,
                            page: r.data.page + 1
                        }) : r.setData({
                            referral_list: e.concat(i),
                            referral_meta: l,
                            bonus_paid_amount: l.bonus_paid_amount,
                            total_referrals: l.total_referrals,
                            hasMore: !1
                        }));
                    }
                }
            });
        }
    },
    onShareAppMessage: function(a) {
        return "button" === a.from && console.log(a.target), {
            title: "全球最大母语私教共享平台，受邀注册即送12美元！",
            path: "/pages/referral/referral?encrypto=" + this.data.referral_meta.encrypto_user_id,
            imageUrl: "../../images/referral/referral-share.png",
            success: function(a) {
                console.log("sucess");
            },
            fail: function(a) {
                console.log("fail");
            }
        };
    },
    phoneinput: function(a) {
        var e = a.detail.value;
        if (/1[3|5|7|8|][0-9]{9}/.test(e)) t = ""; else var t = "请输入正确的手机号码";
        this.setData({
            cellphone: e,
            errorInfo: t
        });
    },
    captchaCodeInput: function(a) {
        var e = a.detail.value;
        this.setData({
            captcha_code: e
        });
    },
    countDown: function() {
        var a = this, e = this.data.timeFlag, t = setInterval(function() {
            e > 0 && e <= 60 ? (e--, a.setData({
                captchaText: e + "s"
            }), a.setData({
                waiting: !0
            })) : (clearInterval(t), e = 60, a.setData({
                captchaText: "重新发送"
            }), a.setData({
                waiting: !1
            }));
        }, 1e3);
    },
    getCaptcha: function() {
        var a = this;
        if (this.data.cellphone) {
            var t = {
                phonenumber: this.data.cellphone,
                phonenumber_country_prefix: "+86",
                type: "verify"
            };
            e.default.getCaptchaViaPhone({
                data: t,
                success: function(e) {
                    e && e.data.data.success && a.countDown();
                }
            });
        } else {
            this.setData({
                errorInfo: "请输入手机号码"
            });
        }
    },
    bindGetUserInfo: function(a) {
        var t = this;
        if (this.data.cellphone) if (this.data.captcha_code) {
            var r = wx.getStorageSync("userInfo").unionid;
            r ? this.signViaPhone(r) : wx.login({
                success: function(a) {
                    var r = a.code;
                    a.code && wx.getUserInfo({
                        success: function(a) {
                            var n = a, i = {
                                code: r,
                                wechatUserDict: JSON.stringify(n)
                            };
                            e.default.loginViaWechat({
                                method: "POST",
                                data: i,
                                success: function(a) {
                                    if (a && a.data.data) {
                                        var e = a.data.data;
                                        if (e.unionid) console.log("union::", e.unionid), wx.setStorage({
                                            key: "userInfo",
                                            data: e
                                        }), t.signViaPhone(e.unionid); else {
                                            var r = a.header["x-token"] || a.header["X-Token"], n = a.header["v-token"] || a.header["V-Token"], i = a.data.data;
                                            i.user_avatar_file = o.CDN_ADDR + i.avatar_file_name + "_Avatar.jpg", s.globalData.userPersonalInfo = i, 
                                            s.globalData.token = r, s.globalData.vtoken = n, wx.setStorage({
                                                key: "userPersonalInfo",
                                                data: i
                                            }), wx.setStorage({
                                                key: "token",
                                                data: r
                                            }), wx.setStorage({
                                                key: "vtoken",
                                                data: n
                                            }), wx.switchTab({
                                                url: "../teacher/teacher"
                                            });
                                        }
                                    }
                                }
                            });
                        }
                    });
                }
            });
        } else {
            n = "请获取验证码";
            this.setData({
                errorInfo: n
            });
        } else {
            var n = "请输入手机号码";
            this.setData({
                errorInfo: n
            });
        }
    },
    signViaPhone: function(a) {
        var t = this, n = this.data.cellphone, i = "mobile" + n.substr(n.length - 4), l = {
            ref: this.data.referral_decrypto_id,
            unionid: a,
            utm_medium: "voucher",
            phone_number_country_prefix: "+86",
            phone_number: n,
            captcha_code: this.data.captcha_code,
            nickname: i
        };
        e.default.signViaPhone({
            method: "POST",
            data: l,
            success: function(a) {
                if (a && a.data.data) {
                    var e = a.header["x-token"] || a.header["X-Token"], n = a.header["v-token"] || a.header["V-Token"], i = a.data.data;
                    i.user_avatar_file = o.CDN_ADDR + i.avatar_file_name + "_Avatar.jpg", i.purchase_sum = r.default.convertCurrencyToCNY(i.purchase_sum, s.globalData.currencyDatum), 
                    s.globalData.userPersonalInfo = i, s.globalData.token = e, s.globalData.vtoken = n, 
                    wx.setStorage({
                        key: "userPersonalInfo",
                        data: i
                    }), wx.setStorage({
                        key: "token",
                        data: e
                    }), wx.setStorage({
                        key: "vtoken",
                        data: n
                    }), t.setData({
                        userPersonalInfo: i,
                        hasUserInfo: !0
                    }), console.log("sing"), wx.switchTab({
                        url: "../teacher/teacher"
                    });
                } else {
                    if ("ErrorCaptcha" === a.data.error.type) l = "验证码错误"; else var l = a.data.error.msg;
                    t.setData({
                        errorInfo: l,
                        hasUserInfo: !1
                    });
                }
            }
        });
    },
    goTeacher: function() {
        wx.switchTab({
            url: "../teacher/teacher"
        });
    }
});